﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;
using CNCSoft.EData;

namespace CNCSoft.ErpApps.TMS.longACSS
{
    public partial class frmKetQua_MidFinalTerm : Form
    {
        SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        DataSet ds = new DataSet();

        public frmKetQua_MidFinalTerm()
        {
            InitializeComponent();
        }

        private void frmKetQua_MidFinalTerm_Load(object sender, EventArgs e)
        {
            LoadControl();
        }

        private void btn_CapNhat_Click(object sender, EventArgs e)
        {
            UpdateData();
            if (CheckDuplicateType(GetLastestInvoice()))
            {
                MessageBox.Show("Dữ liệu bạn vừa nhập đã trùng với thể loại thi của thí sinh này trong hệ thống! Yêu cầu bạn vui lòng nhập lại.");
                string sql = @"delete KetQua_MidFinalTerm where ID in (select top(1) ID from KetQua_MidFinalTerm order by ID desc)";
                SQLHelper.ExecuteNonQuery(conn, sql);
                LoadControl();
            }
        }

        private void btn_BoQua_Click(object sender, EventArgs e)
        {
            ds.RejectChanges();
        }

        #region "Coding"
        private void LoadControl()
        {
            LoadData();
        }

        private void LoadData()
        {
            try
            {
                LoadInvoice();
                LoadKetQua();
                gridControl_Data.DataSource = ds.Tables["TableKetQua"];
                repositoryItemSearchLookUpEdit1.DataSource = ds.Tables["TableInvoice"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadInvoice()
        {
            try
            {
                if (ds.Tables["TableInvoice"] != null)
                    ds.Tables["TableInvoice"].Clear();
                string sql = @"select inv.InvoiceNumber, (enq.Holot + ' ' + enq.Ten + ' - ' + dmkh.TenKhoaHoc) as Display
                                from
                                Invoice inv
                                inner join DM_KHOAHOC dmkh on dmkh.MaKhoaHoc = inv.MaLop
                                inner join Enquiry enq on enq.EnquiryNumber = inv.EnquiryNumber";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableInvoice");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadKetQua()
        {
            try
            {
                if (ds.Tables["TableKetQua"] != null)
                    ds.Tables["TableKetQua"].Clear();
                string sql = @"select * from KetQua_MidFinalTerm";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableKetQua");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void UpdateData()
        {
            try
            {
                string sql = @"select * from KetQua_MidFinalTerm";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                DataHelper.buildDataCommands(ref da);
                da.Update(ds, "TableKetQua");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private bool CheckDuplicateType(string invoicenumber)
        {
            try
            {
                int i = 0, k = 0;
                string sql1 = @"select COUNT(*) from KetQua_MidFinalTerm where InvoiceNumber = '"+invoicenumber+"' and [Type] = 'MidTerm'";
                i = Convert.ToInt32(SQLHelper.ExecuteScalar(conn, sql1));
                string sql2 = @"select COUNT(*) from KetQua_MidFinalTerm where InvoiceNumber = 'EN1205@043' and [Type] = 'FinalTerm'";
                k = Convert.ToInt32(SQLHelper.ExecuteScalar(conn, sql2));
                if (k > 1 || i > 1)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        private string GetLastestInvoice()
        {
            try
            {
                string sql = @"select top(1) InvoiceNumber from KetQua_MidFinalTerm order by ID desc";
                return SQLHelper.ExecuteScalar(conn, sql).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }
        #endregion
    }
}
